package models

import (
	"time"

	"database/sql"
	"log"

	_ "github.com/go-sql-driver/mysql"
)

type stu_series struct {
	Name      string `json:"name"`
	Type      string `json:"type"`
	Stack     string `json:"stack"`
	AreaStyle struct {
		Normal struct{} `json:"normal"`
	} `json:"areaStyle"` //这里会填充线条
	Label struct {
		Normal struct {
			Show     bool   `json:"show"`
			Position string `json:"position"`
		} `json:"normal"`
	} `json:"label"`
	Data []float64 `json:"data"`
}

type Stu_Myddc_Data1 struct { //百度的区域图表数据
	Legend_data []string     `json:"lengend_data"` //统计的几个指标数据，如['投影机使用满意度', '电脑使用满意度', '扩音系统满意度', '服务满意度', '综合满意度']
	XAxis_data  []string     `json:"xAxis_data"`   //日期的X座标
	Series      []stu_series `json:"series"`       //百度图表里series里的多项数据
}

type Stu_Myddc_Data2 struct { //百度的关系图表数据
	TimeData   []string
	YhmydData  []int //与日期对应的用户满意度数据
	YwshulData []int //与日期对应的运维工作量数据
}

func init() {
	//init
}

func (this *Stu_Myddc_Data1) Get_TuBiao1(start time.Time, end time.Time) error {
	type vTmp1 struct {
		Id   int
		Name string
	}
	type vTmp2 struct {
		Date  string  //日期
		TyMyd float64 //投影满意度
		TnMyd float64 //电脑满意度
		KyMyd float64 //扩音满意度
		FwMyd float64 //服务满意度
		ZhMyd float64 //综合满意度
	}
	var (
		sqlstr string
		rows   *sql.Rows

		err error
	)
	/*不单独求出日期，使用满意度调查里的日期列表
	//求出时间段内的所有日期列表
	var dates  []time.Time
	dates = RiQiDuan(start, end)
	for _, v := range dates {
		//log.Println("日期段列表,k:", k, "\t v:", v.Format("2006-01-02"))
		this.XAxis_data = append(this.XAxis_data, v.Format("2006-01-02")) //求出时间列表
	}
	*/

	//求出满意度调查的调查项列表
	sqlstr = "select sys_id,sys_name from view_myddc_type_list"
	rows, err = db.Query(sqlstr)
	if err != nil {
		//log.Println("求出满意度调查的调查项列表，执行数据库查询时出错! err:", err)
		return err
	}
	i := 0
	var _t vTmp1
	for rows.Next() {
		err = rows.Scan(&_t.Id, &_t.Name)
		if err != nil {
			//log.Println("求出满意度调查的调查项列表，读取数据行时出错! err:", err)
			return err
		}
		//log.Println("读取的调查项列表,id:", _t.Id, "\t Name:", t.Name)
		this.Legend_data = append(this.Legend_data, _t.Name)
		this.Series = append(this.Series, stu_series{})
		this.Series[i].Name = _t.Name
		this.Series[i].Type = "line" //百度图表里，这个选项一定要，否则不出图
		this.Series[i].Stack = "总量"
		i++
	}
	//定义图表的总量的百分比数据
	this.Legend_data = append(this.Legend_data, "综合满意度")
	this.Series = append(this.Series, stu_series{})
	this.Series[i].Name = "综合满意度"
	this.Series[i].Type = "line" //百度图表里，这个选项一定要，否则不出图
	this.Series[i].Stack = "总量"
	this.Series[i].Label.Normal.Position = "top"
	this.Series[i].Label.Normal.Show = true
	//定义图表的总量的百分比数据

	//求出每种调查项的满意度指标
	sqlstr = "select vDate,tymyd,tnmyd,kymyd,fwmyd from view_myddc_sjtj_myd1 where unix_timestamp(vDate) between unix_timestamp(?) and unix_timestamp(?)"
	//sqlstr = "select cast(`view_myddc`.`SJ` as date) AS `vDate`,cast(((count((case `view_myddc`.`17` when '满意' then `view_myddc`.`myddc_id` end)) / (count((case `view_myddc`.`17` when '满意' then `view_myddc`.`myddc_id` end)) + count((case `view_myddc`.`17` when '不满意' then `view_myddc`.`myddc_id` end)))) * 100) as decimal(10,2)) AS `tymyd`,cast(((count((case `view_myddc`.`18` when '满意' then `view_myddc`.`myddc_id` end)) / (count((case `view_myddc`.`18` when '满意' then `view_myddc`.`myddc_id` end)) + count((case `view_myddc`.`18` when '不满意' then `view_myddc`.`myddc_id` end)))) * 100) as decimal(10,2)) AS `tnmyd`,cast(((count((case `view_myddc`.`19` when '满意' then `view_myddc`.`myddc_id` end)) / (count((case `view_myddc`.`19` when '满意' then `view_myddc`.`myddc_id` end)) + count((case `view_myddc`.`19` when '不满意' then `view_myddc`.`myddc_id` end)))) * 100) as decimal(10,2)) AS `kymyd`,cast(((count((case `view_myddc`.`20` when '满意' then `view_myddc`.`myddc_id` end)) / (count((case `view_myddc`.`20` when '满意' then `view_myddc`.`myddc_id` end)) + count((case `view_myddc`.`20` when '不满意' then `view_myddc`.`myddc_id` end)))) * 100) as decimal(10,2)) AS `fwmyd` from `view_myddc` where ? < SJ < ? group by cast(`view_myddc`.`SJ` as date)"
	rows, err = db.Query(sqlstr, start.Format("2006-01-02"), end.Format("2006-01-02"))
	if err != nil {
		log.Println("求出调查项‘投影’的满意度指标，执行数据库查询时出错! err:", err)
		return err
	}
	var _t1 vTmp2
	for rows.Next() {
		err = rows.Scan(&_t1.Date, &_t1.TyMyd, &_t1.TnMyd, &_t1.KyMyd, &_t1.FwMyd)
		if err != nil {
			log.Println("求出调查项的满意度指标，读取数据行时出错! err:", err)
			return err
		}
		//log.Println("求出满意度指标，Date:", _t1.Date, "，投影满意度：", _t1.TyMyd, "，电脑满意度：", _t1.TnMyd, "，扩音满意度：", _t1.KyMyd, "，服务满意度：", _t1.FwMyd)
		this.XAxis_data = append(this.XAxis_data, _t1.Date)
		this.Series[0].Data = append(this.Series[0].Data, _t1.TyMyd)
		this.Series[1].Data = append(this.Series[1].Data, _t1.TnMyd)
		this.Series[2].Data = append(this.Series[2].Data, _t1.KyMyd)
		this.Series[3].Data = append(this.Series[3].Data, _t1.FwMyd)
		_t1.ZhMyd = (_t1.TyMyd + _t1.TnMyd + _t1.KyMyd + _t1.FwMyd) / 4
		this.Series[4].Data = append(this.Series[4].Data, _t1.ZhMyd)
	}

	return err
}

func (this *Stu_Myddc_Data2) Get_TuBiao2(start_year int) {

}

//求出一个日期段的所有日期数
func RiQiDuan(start time.Time, end time.Time) (dates []time.Time) {
	add24hour, _ := time.ParseDuration("+24h") //定义一个增加24小时的结构
	for start.Unix() <= end.Unix() {
		//log.Println("自增操作：", start.Format("2006-01-02"))
		dates = append(dates, start)
		start = start.Add(add24hour) //增加24小时
	}
	return dates
}
